package com.ys.nms.module.business.controller.admin.redData;

import lombok.Data;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多列RRD查询结果
 */
@Data
public class RrdMultiQueryResult {
    private Boolean success;
    private String message;
    private List<RrdMultiDataPoint> dataPoints;
    private RrdQueryParams queryParams;
    private List<String> columnNames;          // 列名列表
    private Integer totalPoints;
    private Map<String, Integer> validPointsPerColumn; // 每列的有效数据点数
    private Map<String, Object> additionalInfo;

    private List<String> dateTime;
    private List<Double> data;

    public RrdMultiQueryResult(List<RrdMultiDataPoint> dataPoints,
                               RrdQueryParams queryParams,
                               List<String> columnNames) {
        this.success = true;
        this.message = "查询成功";
        this.dataPoints = dataPoints;
        this.queryParams = queryParams;
        this.columnNames = columnNames;
        this.totalPoints = dataPoints.size();
        this.validPointsPerColumn = calculateValidPointsPerColumn(dataPoints, columnNames);
    }

    public RrdMultiQueryResult(String errorMessage, RrdQueryParams queryParams) {
        this.success = false;
        this.message = errorMessage;
        this.dataPoints = Collections.emptyList();
        this.queryParams = queryParams;
        this.columnNames = Collections.emptyList();
        this.totalPoints = 0;
        this.validPointsPerColumn = Collections.emptyMap();
    }

    private Map<String, Integer> calculateValidPointsPerColumn(List<RrdMultiDataPoint> dataPoints,
                                                               List<String> columnNames) {
        Map<String, Integer> validCounts = new HashMap<>();
        for (String column : columnNames) {
            long count = dataPoints.stream()
                    .filter(point -> point.isMetricValid(column))
                    .count();
            validCounts.put(column, (int) count);
        }
        return validCounts;
    }
}